import { createWebHistory, createRouter } from 'vue-router'

import Login from '@/views/Login/index.vue'
import Home from '@/views/Home/index.vue'
import { useUserStore } from '@/stores/user'
const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            component: Login,
            meta: { isPublic: true }  // 
        },
        {
            path: '/home',
            component: Home,
            meta: { requiresAuth: true }
        }

    ]
})
router.beforeEach((to) => {
    const userStore = useUserStore()

    if (to.meta.requiresAuth) {
        if (!userStore.isLogin()) {
            // ✅ 带原始路径的重定向（用于登录后返回）
            return {
                path: '/',
                replace: true
            }
        }
        return true
    }

    if (to.meta.isPublic && userStore.isLogin()) {
        return { path: '/home', replace: true }
    }

    return true
})

export default router